<template>
    <div class="addCustomModule" v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)">
        <div class="head">
            <div class="left">
                <i class="iconfont icon-zhuangxiu2"></i>
                <span>{{ nowType === "add" ? "新增" : "编辑" }}组件</span>
            </div>
            <div class="right">
                <el-button icon="el-icon-close" title="点击关闭" @click="closeThisWrapper"></el-button>
            </div>
        </div>
        <div class="body">
            <p class="title">自定义组件模式</p>
            <div class="row1">
                <div class="item1">
                    <span class="left required">组件名称：</span>
                    <el-input type="text" class="name" v-model="item.componentName" maxlength="20" show-word-limit placeholder="请输入组件名称" clearable></el-input>
                </div>
                <div class="item2">
                    <span class="left required">组件代码：</span>
                    <el-input type="text" class="componentKey" placeholder="如名称的英文字母形式" v-model="item.componentKey" maxlength="30" show-word-limit clearable></el-input>
                </div>
            </div>
            <div class="row2">
                <div class="item1">
                    <span class="left required">组件类型：</span>
                    <el-select class="type" v-model="item.componentTypeId" clearable placeholder="请选择组件类型">
                        <el-option v-for="cs in componentStyle" :key="cs.id" :label="cs.componentTypeName" :value="cs.id"></el-option>
                    </el-select>
                </div>
                <div class="item2">
                    <span class="left required">组件排序：</span>
                    <el-input type="number" class="sort" v-model="item.sort" :maxlength="10" show-word-limit clearable></el-input>
                </div>
            </div>
            <div class="row3">
                <div class="item1">
                    <span class="left required">上传自定义图标：</span>
                    <div class="right">
                        <div class="buttonWrapper">
                            <div class="uploadBtn" v-if="!item.icon" @click="openChooseIcon">
                                <i class="el-icon-plus"></i>
                            </div>
                            <i v-if="item.icon" :class="item.icon" style="width: 100%; display: flex; justify-content: center"></i>

                            <span v-if="item.icon" class="repaceImg" @click="updateChooseIcon">替换</span>

                            <span v-if="item.icon" class="el-icon-error delImg" @click="delIcon"></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row4">
                <el-button class="submit" type="primary" @click="submitHandle">提 交</el-button>
            </div>
        </div>

        <ChooseImageComponent></ChooseImageComponent>
    </div>
</template>
<script>
import ChooseImageComponent from "@/pages/components/vlonChooseImage/Index.vue";
import Bus from "@/utils/bus.js";
import { getToken } from "@/utils/auth";
import headApi from '@/utils/hade';
export default {
    name: "addCustomModule",
    data() {
        return {
            pageName:"新增自定义组件",
            loading: false,
            nowType: "add",
            item: {
                componentKey: "",
                componentName: "",
                componentTypeId: 1,
                icon: "",
                id: null,
                sort: 0,
            },
            componentStyle: [],
        };
    },
    created() {},
    props: [],
    components: {
        ChooseImageComponent,
    },
    mounted() {
        this.$nextTick(() => {
            this.componentStyle =
                this.$parent.$refs.customModuleComponent.componentStyle;
        });
        Bus.$off("addNewComponentChooseIconAfter2022");
        Bus.$on("addNewComponentChooseIconAfter2022", (params) => {
            this.item.icon = params.url;
        });
    },
    methods: {
        //选择组件类型
        elUploadOnChangeHandle(e) {
            //console.log(e);
        },
        //关闭当前模块
        closeThisWrapper() {
            Bus.$emit("editCustomModule", { show: false });
        },
        //设为默认
        setDefault() {
            this.item = {
                componentKey: "",
                componentName: "",
                componentTypeId: 0,
                icon: "",
                id: null,
                sort: 0,
            };
        },
        //提交保存
        submitHandle() {
            if (!this.item.componentName) {
                this.$message.warning("请输入组件名称");
                return false;
            }
            if (!this.item.componentKey) {
                this.$message.warning("请输入组件代码");
                return false;
            }
            if (!this.item.componentTypeId) {
                this.$message.warning("请选择组件类型");
                return false;
            }
            if (!this.item.sort && this.item.sort !== 0) {
                this.$message.warning("请输入组件排序");
                return false;
            }
            if (!this.item.icon) {
                this.$message.warning("请选择组件图标");
                return false;
            }
            if (this.item.id) {
                this.saveEdit();
            } else {
                this.saveCreate();
            }
        },
        //保存新增
        saveCreate() {
            if (getToken()) {
                this.loading = true;
                this.axios
                    .post(
                        `${headApi}/appComponents`,
                        {
                            componentKey: this.item.componentKey,
                            componentName: this.item.componentName,
                            componentTypeId: this.item.componentTypeId,
                            icon: this.item.icon,
                            sort: this.item.sort,
                        },
                        { headers: { Authorization: "Bearer " + getToken() } }
                    )
                    .then((res) => {
                        this.loading = false;
                        //console.log(res);
                        if (res && res.data) {
                            let { code, data, msg } = res.data;
                            if (code === 200) {
                                this.$message.success("提交成功");
                                setTimeout(() => {
                                    this.setDefault();
                                    this.closeThisWrapper();
                                    this.$parent.$refs.customModuleComponent.init();
                                }, 1500);
                            } else {
                                this.$message.warning(msg);
                            }
                        } else {
                            this.$message.warning("接口请求失败");
                        }
                    })
                    .catch((err) => {
                        //console.log(err);
                        this.loading = false;
                    });
            }
        },
        //保存编辑
        saveEdit() {
            if (getToken()) {
                this.loading = true;
                this.axios
                    .put(
                        `${headApi}/appComponents`,
                        {
                            componentKey: this.item.componentKey,
                            componentName: this.item.componentName,
                            componentTypeId: this.item.componentTypeId,
                            icon: this.item.icon,
                            id: this.item.id,
                            sort: this.item.sort,
                        },
                        { headers: { Authorization: "Bearer " + getToken() } }
                    )
                    .then((res) => {
                        this.loading = false;
                        //console.log(res);
                        if (res && res.data) {
                            let { code, data, msg } = res.data;
                            if (code === 200) {
                                this.$message.success("提交成功");
                                setTimeout(() => {
                                    this.setDefault();
                                    this.closeThisWrapper();
                                    this.$parent.$refs.customModuleComponent.init();
                                }, 1500);
                            } else {
                                this.$message.warning(msg);
                            }
                        } else {
                            this.$message.warning("接口请求失败");
                        }
                    })
                    .catch((err) => {
                        //console.log(err);
                        this.loading = false;
                    });
            }
        },
        //打开弹窗，选择图标
        openChooseIcon() {
            Bus.$emit("openChooseImgGroup", {
                ename: "addNewComponentChooseIcon",
                onlyIcon: true,
                controlid: "2022",
            });
        },
        //打开弹窗，修改图标
        updateChooseIcon() {
            Bus.$emit("openChooseImgGroup", {
                ename: "addNewComponentChooseIcon",
                onlyIcon: true,
                controlid: "2022",
            });
        },
        //删除图标
        delIcon() {
            this.item.icon = "";
        },
    },
    computed: {},
    watch: {},
};
</script>
<style scoped lang="scss">
.addCustomModule {
    position: absolute;
    left: 0;
    box-sizing: border-box;
    top: 0;
    width: 100%;
    padding: 20px;
    height: 100%;
    z-index: 10;
    background-color: #fff;
    & > .head {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-bottom: 20px;
        height: 48px;
        border-bottom: 1px solid #eee;
        margin-bottom: 20px;
        .left {
            i {
                color: #1890ff;
                margin-right: 5px;
                font-size: 15px;
            }
            span {
                font-size: 16px;
            }
        }
        .right {
            .el-button {
                border: none;
            }
            .el-button::after {
                border: none;
            }
        }
    }
    & > .body {
        height: calc(100% - 50px);
        display: flex;
        flex-direction: column;
        .title {
            width: 100%;
            height: 30px;
            display: flex;
            margin-bottom: 50px;
            justify-content: center;
            align-items: center;
        }
        .row1 {
            width: 100%;
            height: 50px;
            display: flex;
            margin-bottom: 50px;
            align-items: center;
            .item1 {
                width: 50%;
                display: flex;
                height: 50px;
                align-items: center;
                justify-content: flex-end;
                .left {
                    position: relative;
                    font-size: 14px;
                    &.required::before {
                        content: "*";
                        position: absolute;
                        left: -10px;
                        top: 4px;
                        color: red;
                    }
                }
                .name {
                    width: 240px;
                    margin-right: 30px;
                }
            }
            .item2 {
                width: 50%;
                display: flex;
                height: 50px;
                align-items: center;
                justify-content: flex-start;
                .left {
                    position: relative;
                    margin-left: 30px;
                    font-size: 14px;
                    &.required::before {
                        content: "*";
                        position: absolute;
                        left: -10px;
                        top: 4px;
                        color: red;
                    }
                }
                .componentKey {
                    width: 240px;
                }
            }
        }
        .row2 {
            width: 100%;
            height: 50px;
            margin-bottom: 50px;
            display: flex;
            align-items: center;
            .item1 {
                width: 50%;
                display: flex;
                height: 50px;
                align-items: center;
                justify-content: flex-end;
                .left {
                    position: relative;
                    font-size: 14px;
                    &.required::before {
                        content: "*";
                        position: absolute;
                        left: -10px;
                        top: 4px;
                        color: red;
                    }
                }
                .type {
                    width: 240px;
                    margin-right: 30px;
                }
            }
            .item2 {
                width: 50%;
                display: flex;
                height: 50px;
                align-items: center;
                justify-content: flex-start;
                .left {
                    position: relative;
                    margin-left: 30px;
                    font-size: 14px;
                    &.required::before {
                        content: "*";
                        position: absolute;
                        left: -10px;
                        top: 4px;
                        color: red;
                    }
                }

                .sort {
                    width: 240px;
                }
            }
        }
        .row3 {
            width: 100%;
            height: 50px;
            display: flex;
            margin-bottom: 100px;
            align-items: center;
            .item1 {
                width: 50%;
                display: flex;
                height: 50px;
                justify-content: flex-end;
                .left {
                    position: relative;
                    display: flex;
                    align-items: center;
                    font-size: 14px;
                    height: 60px;
                    &.required::before {
                        content: "*";
                        position: absolute;
                        left: -8px;
                        top: 50%;
                        margin-top: -5px;
                        color: red;
                    }
                }
                .right {
                    width: 240px;
                    margin-right: 30px;
                    .buttonWrapper {
                        position: relative;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        height: 62px;
                        background-color: #fff;
                        width: 62px;
                        border: 1px solid #f5f5f5;
                        border-radius: 4px;
                        & > i {
                            font-size: 36px;
                        }
                        .uploadBtn {
                            width: 100%;
                            height: 100%;
                            display: flex;
                            box-sizing: border-box;
                            border-radius: 2px;
                            justify-content: center;
                            align-items: center;
                            background-color: #fff;
                            flex-direction: column;
                            position: relative;
                            cursor: pointer;
                            border: 1px solid #e9edef;
                            i {
                                color: #0079fe;
                                font-weight: bold;
                                font-size: 14px;
                                margin-top: 5px;
                                margin-bottom: 5px;
                            }
                        }
                        img {
                            width: 100%;
                            height: 100%;
                            display: flex;
                        }
                        .repaceImg,
                        .delImg {
                            position: absolute;
                            z-index: 3;
                            opacity: 0.5;
                            display: none;
                            cursor: pointer;
                            &:hover {
                                cursor: pointer;
                            }
                        }
                        .repaceImg {
                            left: 0;
                            bottom: 0px;
                            width: 100%;
                            height: 20px;
                            font-size: 12px;
                            background-color: rgba(0, 0, 0, 0.8);
                            color: #fff;
                            justify-content: center;
                        }
                        .delImg {
                            right: -4px;
                            top: -4px;
                        }
                        &:hover {
                            .repaceImg,
                            .delImg {
                                display: flex;
                            }
                        }
                    }
                }
            }
        }
        .row4 {
            width: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            .submit {
                width: 130px;
            }
        }
    }
}
</style>
<style lang="scss">
.addCustomModule {
    .name.el-input {
        input {
            padding-right: 70px;
        }
    }
}
</style>
